package com.aaa.three.service.impl;

import com.aaa.three.entity.AddReg;
import com.aaa.three.entity.Doctor;
import com.aaa.three.entity.Registered;
import com.aaa.three.mapper.ResultMapper;
import com.aaa.three.pojo.domain.UserEmp;
import com.aaa.three.pojo.query.ResultQuery;
import com.aaa.three.result.AjaxResult;
import com.aaa.three.service.ResultService;
import com.aaa.three.sysconst.UserConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * @Author:lyq
 * @Date:2022/2/2411:14
 */
@Service
public class ResultServiceImpl implements ResultService {
    @Autowired
    private HttpSession session;
    @Autowired
    private ResultMapper resultMapper;
    @Override
    public AjaxResult resultQuery(Doctor doctor) {
        UserEmp emp = (UserEmp) session.getAttribute(UserConst.USER_IN_SESSION);
        Integer id = emp.getId();
        doctor.setId(id);
       List<ResultQuery> resultQueryList= resultMapper.queryRegister(doctor);
        Integer sum=resultMapper.sumAll(doctor);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("listAll",resultQueryList);
        map.put("sum",sum);
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult cancel(Integer id) {
     AddReg deleteResult=resultMapper.querySchedule(id);
     Integer i=resultMapper.deleteRegistered(id);
            if(i>0){
                if(deleteResult.getDay()==0){
                    Integer upname = deleteResult.getUpname();
                    Integer a=upname+1;
                    deleteResult.setUpname(a);
                    Integer totalnumber = deleteResult.getTotalnumber();
                    Integer b=totalnumber+1;
                    deleteResult.setTotalnumber(b);
                    Integer lastnumber= deleteResult.getLastnumber();
                    Integer c=lastnumber-1;
                    deleteResult.setLastnumber(c);
                    Integer d= resultMapper.updateUpSchedule(deleteResult);
                    return AjaxResult.success();
                }else{
                    Integer downname = deleteResult.getDownname();
                    Integer a=downname+1;
                    deleteResult.setDownname(a);
                    Integer totalnumber = deleteResult.getTotalnumber();
                    Integer b=totalnumber+1;
                    deleteResult.setTotalnumber(b);
                    Integer lastnumber= deleteResult.getLastnumber();
                    Integer c=lastnumber-1;
                    deleteResult.setLastnumber(c);
                    Integer d= resultMapper.updateDownSchedule(deleteResult);
                    return AjaxResult.success();
                }

            }

        return AjaxResult.fail("取消挂号失败");
    }

    @Override
    public Registered getrecords(Integer id) {
       Registered registered= resultMapper.getrecords(id);
        return registered;
    }
}
